package com.zcsy.manage.service.sms.impl;

import java.util.Hashtable;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

import com.zcsy.commons.sms.dlife.DlifeSmsConfig;
import com.zcsy.commons.sms.dlife.Header;
import com.zcsy.commons.sms.dlife.HttpUtils;
import com.zcsy.commons.sms.dlife.XmlUtils;
import com.zcsy.manage.service.sms.DlifeSmsService;

/**  
 * @Project: service
 * @Title: DlifeSmsServiceImpl.java
 * @Package com.zcsy.manage.service.sms.impl
 * @Description: 发短信接口
 *
 * @author xiangjw 303310059@qq.com
 * @date 2016年5月30日 下午1:42:43
 * @Copyright: 2016 武汉恒数利通软件有限公司 All rights reserved.
 * @version V1.0  
 */
@Service
public class DlifeSmsServiceImpl implements DlifeSmsService{
	
	/**
	 * http正确返回码
	 */
	private static final String HTTP_OK = "200";

		@Override
		public Map<String, String> sendMsg(
				String mobile, String content, String sendTime) {
			Map<String, String> params = new Hashtable<String, String>();
			params.put("action", "send");
			params.put("userid", DlifeSmsConfig.getSMS_USERID());
			params.put("account", DlifeSmsConfig.getSMS_ACCOUNT());
			params.put("password", DlifeSmsConfig.getSMS_PWD());
			params.put("mobile", mobile);
			params.put("content", "【"+DlifeSmsConfig.getSMS_PREFIX()+"】"+content);
			if(!StringUtils.isEmpty(sendTime))
				params.put("sendTime", sendTime);
			Header header = HttpUtils.httpClientPost(DlifeSmsConfig.getSMS_URL(), "UTF-8", params);
			if(HTTP_OK.equals(header.getState())){
				String xml = header.getMsg();
				Element rootElement = XmlUtils.getRootElement(xml,"utf-8");
				Map<String, String> data = new Hashtable<String, String>();
				data.put("returnstatus", XmlUtils.getChildNodeByName(rootElement, "returnstatus").getTextContent());
				data.put("message", XmlUtils.getChildNodeByName(rootElement, "message").getTextContent());
				data.put("remainpoint", XmlUtils.getChildNodeByName(rootElement, "remainpoint").getTextContent());
				data.put("taskID", XmlUtils.getChildNodeByName(rootElement, "taskID").getTextContent());
				data.put("successCounts", XmlUtils.getChildNodeByName(rootElement, "successCounts").getTextContent());
				return data;
			}
			return null;
		}

		@Override
		public Map<String, String> getResidualSum() {
			Map<String, String> params = new Hashtable<String, String>();
			params.put("action", "overage");
			params.put("userid", DlifeSmsConfig.getSMS_USERID());
			params.put("account", DlifeSmsConfig.getSMS_ACCOUNT());
			params.put("password", DlifeSmsConfig.getSMS_PWD());
			Header header = HttpUtils.httpClientPost(DlifeSmsConfig.getSMS_URL(), "UTF-8", params);
			if(HTTP_OK.equals(header.getState())){
				String xml = header.getMsg();
				Element rootElement = XmlUtils.getRootElement(xml,"utf-8");
				Map<String, String> data = new Hashtable<String, String>();
				data.put("returnstatus", XmlUtils.getChildNodeByName(rootElement, "returnstatus").getTextContent());
				data.put("message", XmlUtils.getChildNodeByName(rootElement, "message").getTextContent());
				data.put("payinfo", XmlUtils.getChildNodeByName(rootElement, "payinfo").getTextContent());
				data.put("overage", XmlUtils.getChildNodeByName(rootElement, "overage").getTextContent());
				data.put("sendTotal", XmlUtils.getChildNodeByName(rootElement, "sendTotal").getTextContent());
				return data;
			}
			return null;
		}
}
